# !/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :get_file.py
# @Time      :2025/1/25 14:28:12
# @Author    :flower


import json
import os
import sys

import yaml

from util.get_log import logger


class GetFile:

    @staticmethod
    def get_current_dir():
        return os.getcwd()

    @staticmethod
    def get_root_dir():
        # 获取当前文件的绝对路径
        current_file_path = os.path.abspath(__file__)
        # 获取当前文件所在目录的绝对路径
        current_dir = os.path.dirname(current_file_path)
        while True:
            if os.path.exists(os.path.join(current_dir, 'pytest.ini')):  # 修改为你的项目根目录标识文件
                break
            previous_dir = current_dir
            current_dir = os.path.dirname(current_dir)
            if current_dir == previous_dir:  # 防止无限循环
                break
        return current_dir

    @staticmethod
    # def read_json_file(file_path, base_path='../jsondata/'):
    def read_json_file(file_path, base_path=os.path.dirname(os.path.dirname(os.path.abspath(sys.argv[0])))+os.sep+'jsondata'+os.sep+'common'+os.sep):
        try:
            with open(base_path + file_path, 'r', encoding='utf-8') as file:
                data = json.load(file)  # json文件读取出来时是字典
            logger.info(f'{GetFile.read_json_file.__name__}读取成功, 为 {data} ')
            return data
        except FileNotFoundError:
            logger.debug(f'{GetFile.read_json_file.__name__}读取失败，为 {file_path} 未找到 ')
            return None
        except json.JSONDecodeError:
            logger.debug(f'{GetFile.read_json_file.__name__}读取失败，为 {file_path} 中的JSON数据格式有误 ')
            return None

    @staticmethod
    def read_yaml_file(file_path):
        base_path = '../'
        try:
            with open(base_path + file_path, 'r', encoding='utf-8') as file:
                data = yaml.safe_load(file)
            logger.info(f'{GetFile.read_yaml_file.__name__}读取成功, 为 {data} ')
            return data
        except FileNotFoundError:
            logger.debug(f'{GetFile.read_yaml_file.__name__}读取失败，为 {file_path} 未找到 ')
            return None
        except json.JSONDecodeError:
            logger.debug(f'{GetFile.read_yaml_file.__name__}读取失败，为 {file_path} 中的JSON数据格式有误 ')
            return None

    @staticmethod
    def write_txt_file(data, file_path='token.txt'):
        base_path = '../'
        try:
            with open(base_path + file_path, 'w', encoding='utf-8') as file:
                file.write(data)
            logger.info(f'{GetFile.write_txt_file.__name__}成功, 为 {data} ')
        except FileNotFoundError:
            logger.debug(f'{GetFile.write_txt_file.__name__}失败，为 {file_path} 未找到 ')

    @staticmethod
    def read_txt_file(file_path='token.txt'):
        base_path = '../'
        try:
            with open(base_path + file_path, 'r', encoding='utf-8') as file:
                data = file.read()
            logger.info(f'{GetFile.read_txt_file.__name__}成功, 为 {data} ')
            return data
        except FileNotFoundError:
            logger.debug(f'{GetFile.read_txt_file.__name__}失败，为 {file_path} 未找到 ')
            return None
        except UnicodeDecodeError:
            with open(base_path + file_path, 'r', encoding='GBK') as file:
                data = file.read()
            logger.info(f'{GetFile.read_txt_file.__name__}成功, 为 {data} ')
            return data

    @staticmethod
    def find_data_in_file(file_path='token.txt'):
        data = GetFile.read_txt_file(file_path=file_path)
        lines = data.split("\n")
        for line in lines:
            if "函数" and "运行时间" in line:
                data = line.split("函数")[0]
                return data

if __name__ == '__main__':
    # GetFile.write_txt_file()
    # GetFile.read_txt_file(file_path='zlogs/log')
    GetFile.find_data_in_file(file_path='zlogs/log')

    current_dir = os.getcwd()
    print(current_dir)
    current_dir = GetFile.get_root_dir()
    print(current_dir)
    print(os.path.dirname(os.path.dirname(os.path.abspath(sys.argv[0]))))

